package com.penjin.android.http;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by lxl on 2016/4/8.
 */
public class HttpImage {

    private String uri  ;

    public HttpImage(String uri ){

        this.uri = uri ;

    }

    public void loadImage( final  ImageDownLoadCallback imageDownLoadCallback){

        final Handler handler  = new Handler(){

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Drawable drawable = (Drawable) msg.obj ;

                imageDownLoadCallback.getDrawable(drawable);

            }
        } ;

        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    Drawable drawable = Drawable.createFromStream(new URL(uri).openStream(), "");

                    Message message = Message.obtain();
                    message.obj = drawable;
                    handler.sendMessage(message);
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();

    }

    public interface ImageDownLoadCallback{

        public void getDrawable(Drawable drawable ) ;

    }


}
